<?php
class Model_telestatus extends Model {
    function Model_telestatus() {
        parent::Model();
    }

    /**
     * Functie care aduce datele in grid
     */
    function getData($aPost) {

        $iStart = (isset($aPost["start"]) && ($aPost["start"] != "")) ? $aPost["start"] : 0;
        $iLimit = (isset($aPost["limit"]) && ($aPost["limit"] != "")) ? $aPost["limit"] : 100;
        $sSort = (isset($aPost["sort"]) && ($aPost["sort"] != "")) ? $aPost["sort"] : "ts_name";
        $sDir = (isset($aPost["dir"]) && ($aPost["dir"] != "")) ? $aPost["dir"] : "ASC";

        if (isset($aPost["activ"]) && ($aPost["activ"] == 1)) {
            $this->db->where("ts_active", $aPost["activ"]);
        }
        if (isset($aPost["client_id"]) && ($aPost["client_id"] != "")) {
            $this->db->where("tele_status.client_id", $aPost["client_id"]);
        }

        $this->db->join("client", "client.client_id=tele_status.client_id", "left");
        $this->db->order_by($sSort, $sDir);
        $this->db->limit($iLimit, $iStart);
        $query = $this->db->get("tele_status");

        $totalCount = $this->db->count_all_results("tele_status");
        $aData = $query->result_array();

        $data = array('totalCount' => $totalCount, 'data' => $aData);
        return $data;
    }


    /**
     * Adaugare / editare
     * @param  $aData
     * @return bool|string
     */
    function operation($aData) {
        $aTemp = array(
            "ts_name" => $aData["ts_name"],
            "client_id" => $aData["client_id"],
            "ts_interest" => $aData["ts_interest"],
             "ts_rev" => $aData["ts_rev"],
            "ts_prog" => $aData["ts_prog"],
            "ts_contact_data" => $aData["ts_contact_data"],
            "ts_active" => $aData["ts_active"]
        );
        //verifica duplicatele ( vezi dbase in dir library)
        $bDuplicate = $this->_verifyDuplicat($aData["ts_id"], $aTemp);
        if ($bDuplicate === TRUE) {
            return "Duplicat";
        }
        //insert
        if ($aData["ts_id"] == 0) {
            $this->db->insert("tele_status", $aTemp);
        }
        else {
            $this->db->where("ts_id", $aData["ts_id"]);
            $this->db->update("tele_status", $aTemp);
        }
        return TRUE;
    }

    function _verifyDuplicat($iRecord, $aData) {
        $this->db->where("ts_name", $aData["ts_name"]);
        $this->db->where("client_id", $aData["client_id"]);
        $this->db->where("ts_id !=", $iRecord);
        $iNumRows = $this->db->get("tele_status")->num_rows();
        if ($iNumRows > 0) {
            return TRUE;
        }
        return FALSE;
    }


    /**
     * Functie care aduce datele despre o inregistrare
     */
    function getRecord($aData) {
        $this->db->select("tele_status.*, name as client_id_val");
        $this->db->join("client", "client.client_id=tele_status.client_id", "left");
        $this->db->where("ts_id", $aData["iRec"]);
        return $this->db->get("tele_status")->row_array();
    }
}